package com.woniuxy.carlife.carlifeplatformweb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.carlife.carlifecommonsweb.util.RandomCode;
import com.woniuxy.carlife.carlifeplatformweb.domain.ManagerAccount;
import com.woniuxy.carlife.carlifeplatformweb.mapper.ManagerAccountMapper;
import com.woniuxy.carlife.carlifeplatformweb.param.ManagerAccountLoginParam;
import com.woniuxy.carlife.carlifeplatformweb.param.ManagerAccountRegisterParam;
import com.woniuxy.carlife.carlifeplatformweb.service.ManagerAccountService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.Date;

/**
 * @ClassName: ManagerAccountServiceImpl
 *@Description: 平台实现类
 * @author: huangxinyan     
 * @date: 2021-04-05
 */

@Service
public class ManagerAccountServiceImpl extends ServiceImpl<ManagerAccountMapper, ManagerAccount> implements ManagerAccountService {

    @Resource
    private ManagerAccountService managerAccountService;
    @Resource
    private ManagerAccountMapper managerAccountMapper;
    /**
     * 功能描述〈管理员新增〉
     * @author: huangxinyan
     * @param: ManagerAccountRegisterParam
     * @return: int
     * @Date 2021/4/14 16:44
     * @throws
     */

    @Override
    public int setManagerAccount(ManagerAccountRegisterParam managerAccountRegisterParam) {
        //查询管理员账号是否被注册
        QueryWrapper<ManagerAccount> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("manager_account_name", managerAccountRegisterParam.getManagerAccountName());
        ManagerAccount managerAccount= managerAccountService.getOne(queryWrapper);
        //判断管理员账号是否被注册
        if (ObjectUtils.isEmpty(managerAccount)){
            String salt= RandomCode.getCode(8);
            Md5Hash md5Hash=new Md5Hash(managerAccountRegisterParam.getManagerAccountPassword(),salt,2048);
            //new一个管理员账号类
            ManagerAccount managerAccount1=new ManagerAccount();
            //存入创建时间
            managerAccount1.setGmtCreate(new Timestamp(new Date().getTime()));
            //存入账号状态(默认为1（正常）0 注销)
            managerAccount1.setManagerAccountStatus(1);
            //存入盐
            managerAccount1.setManagerAccountSalt(salt);
            //存入管理员密码
            managerAccount1.setManagerAccountPassword(md5Hash.toHex());
            //存入管理员账户
            managerAccount1.setManagerAccountName(managerAccountRegisterParam.getManagerAccountName());
            //执行管理员新增方法
            managerAccountMapper.insert(managerAccount1);
            //新增成功返回 0
            return 0;
        }else {
            //失败返回 1
            return 1;
        }
    }

    /**
     * 功能描述〈管理员登录〉
     * @author: huangxinyan
     * @param: ManagerAccountLoginParam
     * @return: int
     * @Date 2021/4/14 17:22
     * @throws
     */

    @Override
    public int getManagerAccount(ManagerAccountLoginParam managerAccountLoginParam) {
        if (!ObjectUtils.isEmpty(managerAccountLoginParam)) {
            //根据门店账号查询密码和盐
            QueryWrapper<ManagerAccount> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("manager_account_name", managerAccountLoginParam.getManagerAccountName());
            ManagerAccount managerAccount = managerAccountMapper.selectOne(queryWrapper);
            //判断该账号是否存在
            if(!ObjectUtils.isEmpty(managerAccount)){
                //将传入的密码进行加密
                Md5Hash md5Hash = new Md5Hash(managerAccountLoginParam.getManagerAccountPassword(), managerAccount.getManagerAccountSalt(), 2048);
                String loginPassword = md5Hash.toHex();
                //将加密后的密码和数据库中的密码进行对比,登录成功返回0，失败返回1
                if (loginPassword.equals(managerAccount.getManagerAccountPassword())) {
                    return 0;
                } else {
                    return 1;
                }
            }else {
                return 2;
            }

        }
        //账号或密码为空返回 2
        return 2;
    }
}
